import {
	WARN_LEVEL_MAP,
	WARNING_DEDUCTION_RULE_MAP,
	TASK_CATEGORY_TYPES_MAP,
} from './constants';

export default function calculateScore(categoriesWarning) {
	const eachPercent = 100 / categoriesWarning.length;
	return Math.round(
		categoriesWarning.reduce((prevRate, c) => {
			const rate = c.reduce((res, w) => {
				res -= WARNING_DEDUCTION_RULE_MAP[WARN_LEVEL_MAP[w.level]];
				return res > 0 ? res : 0;
			}, eachPercent);

			return rate + prevRate;
		}, 0)
	);
}

export function calculateJobScore(job) {
	const warnings = [
		TASK_CATEGORY_TYPES_MAP.weakness,
		TASK_CATEGORY_TYPES_MAP.securityEvent,
		TASK_CATEGORY_TYPES_MAP.content,
	].map(categoies =>
		categoies
			.reduce((arr, taskType) => {
				return arr.concat(job[`${taskType}Result`]?.result?.warnings || []);
			}, [])
			.filter(w => !w?.isResolved && !w?.ignoredTime)
	);

	return calculateScore(warnings);
}

export function levelLabel(totalRate) {
	if (totalRate > 95) {
		return '风险较低';
	} else if (totalRate <= 95 && totalRate >= 70) {
		return '风险较高';
	} else {
		return '风险严重';
	}
}
